package weaver.interfaces.xinri.duss.action;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.xinri.duss.utils.InvokeHelper;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-03-21
 * @Description: 单据流程当前审批人审批驳回
 * @ModifyLog:
 **/
public class WeaApprovalsRejectAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String fullDate = DateUtil.getFullDate();
        String src = requestInfo.getRequestManager().getSrc();
        String clientIp = requestInfo.getRequestManager().getIp();
        writeLog(">>>>>WeaApprovalsRejectAction>>>>>requestid=" + requestid);
        try {
            JSONObject paramjson = new JSONObject();
            rs.executeQuery("select dh,gh1,entityType,sfoath from " + tablename + " where requestid=?", requestid);
            rs.next();
            String dh = Util.null2String(rs.getString("dh"));
            String gh1 = Util.null2String(rs.getString("gh1"));
            String entityType = Util.null2String(rs.getString("entityType"));
            String sfoath = Util.null2String(rs.getString("sfoath"));
            paramjson.put("approvalTxt", "不通过");
            paramjson.put("operator", gh1);
            paramjson.put("approver", gh1);
            paramjson.put("entityType", entityType);
            paramjson.put("businessCode", dh);
            paramjson.put("createdDate", InvokeHelper.dateFormat(fullDate));
            paramjson.put("lastModifiedDate", InvokeHelper.dateFormat(fullDate));
            if ("0".equals(sfoath)) {
                writeLog(">>>>>WeaApprovalsRejectAction>>>>>paramjson=" + paramjson);
                String resultStr = InvokeHelper.reject(paramjson.toJSONString());
                Map<String,Object> dataMap = new HashMap<String, Object>();
                dataMap.put("jkqqcs",paramjson.toJSONString());
                dataMap.put("jkxyfh",resultStr);
                dataMap.put("khdip",clientIp);
                dataMap.put("gllc",requestid);
                dataMap.put("oacz",src);
                dataMap.put("jkdysj",fullDate);
                dataMap.put("djh", dh);
                dataMap.put("rqsj", DateUtil.getDateTime());
                dataMap.put("jkmc", "2");
                dataMap.put( "lcid", requestid );
                InvokeHelper.insertApiLog(dataMap,"Reject");
                JSONObject resultJson = JSONObject.parseObject(resultStr);
                writeLog(">>>>>WeaApprovalsRejectAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("operation completed".equals(message)) {
                    return Action.SUCCESS;
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("汇联易接口返回异常:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }else {
                writeLog(">>>>>WeaApprovalsRejectAction>>>>>不调用汇联易接口通过");
                return Action.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
